package com.hee.erp.modules.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hee.erp.common.R;
import com.hee.erp.modules.entity.SysDictType;
import com.hee.erp.modules.entity.SysDictValue;
import com.hee.erp.modules.entity.SysJob;
import com.hee.erp.modules.service.ISysDictTypeService;
import com.hee.erp.modules.service.ISysDictValueService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author weizehong
 * @since 2022-03-21
 */
@RestController
@RequestMapping("/modules/sysDictValue")
public class SysDictValueController {

    @Autowired
    ISysDictValueService targetService;

    @Autowired
    ISysDictTypeService sysDictTypeService;

    //四个方法 listByPage,getDetailById,delById,save

    @PostMapping("/listByPage")
    @ApiOperation(value = "listByPage",notes = "{\n" +
            "\"current\":1,\n" +
            "\"pageSize\":10\n" +
            "}")
    public R<IPage<SysDictValue>> listByPage(@RequestBody JSONObject params){
        Integer pageSize = params.getInteger("pageSize");
        Integer current = params.getInteger("current");
        IPage<SysDictValue> page = new Page<>(current,pageSize);
        LambdaQueryWrapper<SysDictValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(SysDictValue::getCreateTime);
        targetService.page(page,queryWrapper);

        List<SysDictValue> records = page.getRecords();
        //获取dictTypeName
        List<String> idList = records.stream().map(entity -> entity.getDictTypeId()).collect(Collectors.toList());
        if(idList.size() > 0){
            LambdaQueryWrapper<SysDictType> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(SysDictType::getId, idList);
            List<SysDictType> dictTypeList = sysDictTypeService.list(wrapper);
            for (SysDictValue record : records) {
                Optional<SysDictType> first = dictTypeList.stream().filter(entity -> entity.getId().equals(record.getDictTypeId())).findFirst();
                SysDictType sysDictType = first.orElse(new SysDictType());
                record.setDictTypeName(sysDictType.getTypeName());
            }
        }
        return R.success(page);
    }

    @ApiOperation(value = "getDetailById")
    @GetMapping("/getDetailById")
    public R<SysDictValue> getById(String id){
        SysDictValue entity = targetService.getById(id);
        return R.success(entity);
    }

    @PostMapping("/save")
    @ApiOperation(value = "save")
    public R<Boolean> save(@RequestBody SysDictValue entity){
        targetService.saveOrUpdate(entity);

        return R.success(true);
    }

    @ApiOperation("delById")
    @GetMapping("/delById")
    public R<Boolean> delById(String id){
        targetService.removeById(id);
        return R.success(true);
    }

    @ApiOperation("/getListByTypeValue")
    @GetMapping("/getListByTypeValue")
    public R<List<SysDictValue>> getListByTypeValue(String dictTypeValue){
        List<SysDictValue> list = targetService.getListByTypeValue(dictTypeValue);
        return R.success(list);
    }

}

